#include "StdAfx.h"
#include "LitEditorProject.h"
#include <OgreStringConverter.h>


namespace Lit
{
	namespace Editor
	{
		Project::Project(Instance * inst, QTreeWidget * tree, const std::string &name, const std::string &file_ext)
			: mTree( tree )
			, mInstance( inst )
			, mRoot( NULL )
		{
			mFileExt = file_ext;
			mName = name;
			mCreated = false;
			if( tree )
			{
				tree->header()->setVisible( true );
				tree->setHeaderLabels( QStringList(QString("Property"))<<QString("Value") );
				tree->setColumnWidth( 0, 100 );
				tree->header()->setStretchLastSection(true);
			}
		}

		Project::~Project(void)
		{ destroy(); }
		
		const Instance* Project::getInstance() const{ return mInstance; }
		Instance * Project::getInstance(){ return mInstance; }

		const QTreeWidget * Project::getTreeWidget() const{ return mTree; }
		QTreeWidget * Project::getTreeWidget(){ return mTree; }
			
		const QTreeWidgetItem * Project::getRootTreeNode() const{ return mRoot; }
		QTreeWidgetItem * Project::getRootTreeNode(){ return mRoot; }
		
		const std::string &Project::getName() const{ return mName; }
		
		QTreeWidgetItem * Project::createRootNode( const std::string &name, bool expanded, Qt::ItemFlags flags ){
			QTreeWidgetItem * itm = new QTreeWidgetItem( mRoot, QStringList(TO_QT(name)) );
			mRoot->addChild( itm );
			itm->setExpanded( expanded );
			itm->setFirstColumnSpanned( true );
			itm->setFlags( flags );
			return itm;
		}
		QTreeWidgetItem * Project::createNode( const std::string &name, QTreeWidgetItem * parent, bool expanded, Qt::ItemFlags flags ){
			QTreeWidgetItem * itm = new QTreeWidgetItem( parent, QStringList(TO_QT(name)) );
			parent->addChild( itm );
			itm->setExpanded( expanded );
			itm->setFirstColumnSpanned( false );
			itm->setFlags( flags );
			return itm;
		}
		void Project::setValue( QTreeWidgetItem * node, const std::string &val )
		{
			if( node ) node->setText( 1, TO_QT( val ) );
		}
		void Project::setWidget( QTreeWidgetItem * node, QWidget * w )
		{
			if( node ) mTree->setItemWidget( node, 1, w );
		}
		void Project::destroy()
		{
			_destroyProject( );
			if( mTree )
				mTree->clear();
			mCreated = false;
		}
		void Project::create( const std::string &name )
		{
			if( isCreated() )
				destroy();
			mName = name;
			mRoot = new QTreeWidgetItem( mTree, QStringList(TO_QT(name)) );
			mTree->addTopLevelItem( mRoot );
			mRoot->setExpanded( true );
			_createProject( name );
			mCreated = true;
		}
		
		bool Project::isSavePathDefined() const{ return !mSavePath.empty(); }
		void Project::setSavePath( const std::string &path ){ mSavePath = path; }
		const std::string &Project::getSavePath() const{ return mSavePath; }

		void Project::setFileExtension( const std::string &ext ){ mFileExt = Ogre::StringUtil::replaceAll(ext, ".", ""); }
		const std::string &Project::getFileExtension() const{ return mFileExt; }
	};
};
